//
//  CookingbarCommentController.m
//  zhijianshejian
//
//  Created by admin on 16/9/1.
//  Copyright © 2016年 haoniu-duhuan. All rights reserved.
//

#import "CookingbarCommentController.h"
#import "IQTextView.h"


@interface CookingbarCommentController ()<UITextViewDelegate>{
    IQTextView * textView;
}
@property (nonatomic,strong)UILabel *commentCountLabel;

@end

@implementation CookingbarCommentController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"评价";
    [self.view setBackgroundColor:[AppConfig colorWithHexString:@"#ececec"]];
    
//    UIBarButtonItem * itemLeft = [[UIBarButtonItem alloc]init];
//    [itemLeft setImage:[UIImage imageNamed:@"fanhui"]];
//    [itemLeft setTarget:self];
//    [itemLeft setAction:@selector(backBtnClick)];
//    self.navigationItem.leftBarButtonItem=itemLeft;
    
    UIBarButtonItem * itemRight = [[UIBarButtonItem alloc]init];
    [itemRight setTitle:@"确定"];
    [itemRight setTarget:self];
    [itemRight setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:NSFontAttributeName,[UIFont systemFontOfSize:16], nil] forState:UIControlStateNormal];
    [itemRight setAction:@selector(submiteMethod)];
    self.navigationItem.rightBarButtonItem=itemRight;
    
    textView = [[IQTextView alloc] initWithFrame:CGRectMake(10, 10, mainScreenSize.width-20, 160)];
//    textView.delegate = self;
    [textView setPlaceholder:@"请输入评论内容..."];
    [textView setFont:[UIFont systemFontOfSize:16]];
    [textView.layer setMasksToBounds:YES];
    [textView.layer setCornerRadius:4];
    textView.delegate = self;
    [self addNotificationCenter];
    [self.view addSubview:textView];
    self.commentCountLabel = [[UILabel alloc ]initWithFrame:CGRectMake(mainScreenSize.width-100, 170, 90, 20)];
    self.commentCountLabel.text = @"0/100字";
    self.commentCountLabel.font = [UIFont systemFontOfSize:14];
    self.commentCountLabel.textAlignment =  NSTextAlignmentRight;
    [self.view addSubview:self.commentCountLabel];
}

#pragma -mark UITextViewDelegate

-(void)addNotificationCenter{
    [NotificationCenter addObserver:self selector:@selector(textChange) name:UITextViewTextDidChangeNotification object:textView];
}

-(void)dealloc{
    [NotificationCenter removeObserver:self.view name:UITextViewTextDidChangeNotification object:textView.text];
}

-(void)textChange{
    if (textView.text.length<=100) {
        self.commentCountLabel.text = [NSString stringWithFormat:@"%ld/100字",textView.text.length];
    }else{
        self.commentCountLabel.text = [NSString stringWithFormat:@"已输入100字"];
    }
    
}

#pragma -mark 返回
-(void)backBtnClick{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma -mark 评论
-(void)submiteMethod{
    [self hideHud];
    if (textView.text.length==0) {
        [self showHint:@"请输入评论内容"];
        return;
    }
    [self hideHud];
    [self showHudInView:self.view hint:nil];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithObjectsAndKeys:static_sessionId,@"sessionId",nil];
    
    [param setObject:_orderSn forKey:@"orderSn"];
    [param setObject:textView.text forKey:@"buyerComment"];
    
    NSLog(@"param = %@",param);
    
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager manager] initWithBaseURL:[NSURL URLWithString:url_Base]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager POST:url_comment_add parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSError * error;
        NSLog(@"responseObject = %@",responseObject);
        
        NSDictionary * data=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        [self hideHud];
        if (error) {
            NSLog(@"====error1:%@",error);
            [self showHint:@"评论失败"];
            NSLog(@"==dict:%@",data);
            return;
        }
        NSLog(@"==dict:%@",data);
        
        NSString * result = [NSString stringWithFormat:@"%@",[data objectForKey:@"result"]];
        if ([result isEqualToString:@"1"]) {
       
            [self showHint:@"评论成功"];
            self.view.userInteractionEnabled = NO;
              [self performSelector:@selector(backBtnClick) withObject:nil afterDelay:1.50f];
       
           
        }else{
            NSString * info = [NSString stringWithFormat:@"%@",[data objectForKey:@"message"]];
            [self showHint:info];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull   error) {
        NSLog(@"====error:%@",error);
        [self hideHud];
        [self showHint:@"评论失败"];
    }];
}


@end
